package yang.yu.sunny.application.impl;

import org.dayatang.utils.BeanUtils;
import yang.yu.sunny.application.OrderItemDto;
import yang.yu.sunny.datamodel.OrderItemData;
import yang.yu.sunny.domain.Product;
import yang.yu.sunny.domain.ProductCategory;

import java.util.Map;

/**
 * Created by yyang on 16/3/29.
 */
public class OrderConverter {
    private Map<Long, Product> productMap;
    private Map<Long, ProductCategory> categoryMap;

    public OrderConverter(Map<Long, Product> productMap, Map<Long, ProductCategory> categoryMap) {
        this.productMap = productMap;
        this.categoryMap = categoryMap;
    }

    public OrderItemDto toDto(OrderItemData item) {
        OrderItemDto result = new OrderItemDto();
        new BeanUtils(result).copyPropertiesFrom(item);
        Product product = productMap.get(item.getProductId());
        result.setProductName(product.getName());
        Long categoryId = product.getCategory().getId();
        result.setCategoryId(categoryId);
        result.setCategoryName(categoryMap.get(categoryId).getName());
        return result;
    }

}
